#coding=utf-8
'''
Created on 9.30, 2016
@author: wangpeng
'''
import unittest
from appium import webdriver
from selenium.webdriver.common.by import By   #用于匹配某个值是否出现，智能等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import common.config
import common.logging_save_huawei_honor4A
import common.desired_setting_huawei_honor4A
import threading
import re
import common.Create_Log_Folder
import os
import os.path
import common.logging_save_huawei_honor4A
import serial
#import common.save_serial_log
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import subprocess

class Test(unittest.TestCase):
    def setUp(self):
        os.system('taskkill /f /im adb.exe')
        common.logging_save_huawei_honor4A.logging.info("app测试开始，正在开始启动app")
        print time.ctime(),"app测试开始，正在开始启动app"
        self.begin_time_for_serial = time.strftime("%y%m%d_%H:%M:%S", time.localtime()) 
        self.save_log = common.Create_Log_Folder.Create("\\huawei_honor4A\\factory_setting_speical")    #建立report保存路径
        common.logging_save_huawei_honor4A.logging.info("反复连接当前连接的附件，主要目的是为了验证是否死机，还有是否附件是否连接成功")
        print time.ctime(),"反复连接当前连接的附件，主要目的是为了验证是否死机，还有是否附件是否连接成功"
        sys.path.append('\adbcommand')  #增加adb log
        self.test = subprocess.Popen(r'E:\eclipse\huawei_rader\all_mobile\testcase\huawei_honor4A\adbcommond\factory_setting_speical.bat') #导入adb
        time.sleep(1)
        
        #开启appium设置
        self.desired_setting = common.desired_setting_huawei_honor4A.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_setting)

        time.sleep(5)
        #由于V0.2.3版本突然有一个Bug，一开打app就会弹出来一个更新遥控器，所以临时添加确认下

        common.logging_save_huawei_honor4A.logging.info("app连接BLE中，70秒后再做判定。。。")
        print time.ctime(),"app连接BLE中，70秒再做判定。。。"
        time.sleep(70)
        
        try:
            self.driver.find_element_by_id("com.huawei.smallRadar:id/sure").click()
            time.sleep(3)
            common.logging_save_huawei_honor4A.logging.info("close弹出框")
            print time.ctime(),"close弹出框"

        except:
            pass
        
    def tearDown(self):
        #self.driver.quit()
        time.sleep(1)
        self.test.kill()
        os.system('taskkill /f /im adb.exe')
        print '关闭adb log'
        time.sleep(10)
        print 'finished'
        
    def test_play(self):
        u"""测试步骤：华为荣耀honor4A手机，进入到设置--》工厂复位设置，操作200次，观察是否会死机或者蓝牙断开现象"""
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        #判断是否进入主界面
        try:
            WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.ID,common.config.wendu()))) #确认是否进入到主界面                       
            common.logging_save_huawei_honor4A.logging.info("经过确认，已经进入到主菜单页面")
            print time.ctime(),"经过确认，已经进入到主菜单页面"
            time.sleep(3)
        except:
            self.driver.get_screenshot_as_file(save_pic)
        testtime = 1
        while(testtime <=50):   #验证，次数为200次
            testtime = testtime + 1
            time.sleep(10)
            self.driver.find_element_by_class_name(common.config.menu_setting_click()).click()
            time.sleep(3)
            common.logging_save_huawei_honor4A.logging.info("进入到设置界面")
            print time.ctime(),"进入到设置界面"
            self.driver.find_element_by_xpath(common.config.setting_menu_factory()).click()
            time.sleep(10)
            self.driver.find_element_by_xpath(common.config.huifuchuchang_click()).click()
            time.sleep(2)
            try:
                self.driver.find_element_by_id("com.huawei.smallRadar:id/sure").click()
                time.sleep(3)
                common.logging_save_huawei_honor4A.logging.info("点击恢复出厂设置，正在恢复出厂设置中，请等待70秒。。。。")
                print time.ctime(),"点击恢复出厂设置，正在恢复出厂设置中，请等待70秒。。。。"

            except:
                pass
            time.sleep(70)
            def valiate_ok():
                try:
                    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID,common.config.BLE_disconnect())))
                    common.logging_save_huawei_honor4A.logging.info("发现蓝牙断开，发生在第："+str(testtime)+"次")
                    print time.ctime(),"发现蓝牙断开，发生在第："+str(testtime)+"次"
                    a = 1
                    self.driver.get_screenshot_as_file(save_pic)
                    return a
                                                
                except:
                    common.logging_save_huawei_honor4A.logging.info("经过确认，进入到app连接成功次数为第："+str(testtime)+"次")
                    print time.ctime(),"经过确认，进入到app连接成功次数为第："+str(testtime)+"次"
                    a= 0
                    return a
            
            self.assertNotEqual(valiate_ok(), 1, "发现连接断开现象，请确认问题所在")
            common.logging_save_huawei_honor4A.logging.info("经过确认，蓝牙正常连接成功,次数为"+str(testtime)+'次')
            print time.ctime(),"经过确认，蓝牙正常连接成功,次数为"+str(testtime)+'次'
            
if __name__ == "__main__":
    unittest.main()